package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.annotations.IgnoreClientToken;
import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.condition.dbgrouprel.DbGroupRelCondition;
import com.bwda.dsrs.filecenter.domain.po.dbgroup.DbGroupPo;
import com.bwda.dsrs.filecenter.domain.condition.dbgroup.DbGroupCondition;
import com.bwda.dsrs.filecenter.domain.form.dbgroup.DbGroupQueryForm;
import com.bwda.dsrs.filecenter.domain.form.dbgroup.DbGroupUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.dbgroup.DbGroupCreateForm;
import com.bwda.dsrs.filecenter.domain.po.dbgrouprel.DbGroupRelPo;
import com.bwda.dsrs.filecenter.domain.po.dbinfo.DbInfoPo;
import com.bwda.dsrs.filecenter.domain.vo.dbgroup.DbGroupRelDbNameVo;
import com.bwda.dsrs.filecenter.domain.vo.dbgroup.DbGroupVo;
import com.bwda.dsrs.filecenter.service.DbGroupRelService;
import com.bwda.dsrs.filecenter.service.DbGroupService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.service.DbInfoService;
import com.bwda.dsrs.systemservice.domain.Constants;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;

import static com.bwda.dsrs.base.centerbase.BaseContextHandler.getOrgAdminCode;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/dbgroup")
@Api(value = "/b/dbgroup", tags = "数据库组表")
public class DbGroupController extends BaseController {

	@Autowired
	private DbGroupService dbGroupService;
	@Autowired
	private DbGroupRelService dbGroupRelService;
	@Autowired
	private DbInfoService dbInfoService;

	@ApiOperation(value = "查询数据库组表",notes = "查询数据库组表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DbGroupVo> query(@ApiParam(value = "ID", required = true)@RequestParam Integer id) throws BusinessServiceException {
		DbGroupPo po = dbGroupService.queryWithValid(id);
		DbGroupVo vo = CopyUtil.transfer(po, DbGroupVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询数据库组表数量",notes = "查询数据库组表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid DbGroupQueryForm form) throws BusinessServiceException {
		DbGroupCondition condition = this.getConditionByQueryForm(form);
		int count = dbGroupService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询数据库组表列表",notes = "查询数据库组表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<DbGroupVo>> queryList(@ModelAttribute@Valid DbGroupQueryForm form) throws BusinessServiceException {
		DbGroupCondition condition = this.getConditionByQueryForm(form);
		condition.setDelFlag(Constants.NOT_DELETED);
		condition.setStatus(Constants.ENABLE);
		List<DbGroupPo> poList = dbGroupService.queryList(condition);
		List<DbGroupVo> voList = CopyUtil.transfer(poList, DbGroupVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询数据库组表列表(带分页)",notes = "查询数据库组表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<DbGroupVo>> queryPageList(@ModelAttribute@Valid DbGroupQueryForm form) throws BusinessServiceException {
		DbGroupCondition condition = this.getConditionByQueryForm(form);
		List<DbGroupVo> voList = new ArrayList<>();
		int count = dbGroupService.queryCount(condition);
		if (count > 0) {
			List<DbGroupPo> poList = dbGroupService.queryList(condition);
			voList = CopyUtil.transfer(poList, DbGroupVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增数据库组表",notes = "新增数据库组表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DbGroupVo> add(@ModelAttribute@Valid DbGroupCreateForm form) throws BusinessServiceException {
		DbGroupPo po = CopyUtil.transfer(form, DbGroupPo.class);
//		po.setId(RandomUtils.nextInt());
		dbGroupService.insert(po);
		DbGroupVo vo = CopyUtil.transfer(po, DbGroupVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改数据库组表",notes = "修改数据库组表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid DbGroupUpdateForm form) throws BusinessServiceException {
		DbGroupPo po = CopyUtil.transfer(form, DbGroupPo.class);
		dbGroupService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除数据库组表",notes = "删除数据库组表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "ID", required = true)@RequestParam List<Integer> ids) throws BusinessServiceException {
		if(!CollectionUtils.isEmpty(ids)){
			for (Integer id:ids
					) {
				dbGroupService.delete(id);

			}
		}
		return getSuccessResult();
	}

	@ApiOperation(value = "查询数据库分组列表(带分页)",notes = "查询数据库分组列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryDbGroupList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<DbGroupRelDbNameVo>> queryDbGroupList(@ModelAttribute@Valid DbGroupQueryForm form) throws BusinessServiceException {
		DbGroupCondition condition = this.getConditionByQueryForm(form);
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		List<DbGroupRelDbNameVo> voList = new ArrayList<>();
		int count = dbGroupService.queryCount(condition);
		if (count > 0) {
			List<DbGroupPo> poList = dbGroupService.queryList(condition);
			if(!CollectionUtils.isEmpty(poList)){
				voList = CopyUtil.transfer(poList, DbGroupRelDbNameVo.class);
				for (DbGroupRelDbNameVo dbGroupRelDbNameVo:voList
					 ) {
					DbGroupRelCondition dbGroupRelCondition = new DbGroupRelCondition();
					dbGroupRelCondition.setGroupId(dbGroupRelDbNameVo.getId());
					List<DbGroupRelPo> dbGroupRelPos = dbGroupRelService.queryList(dbGroupRelCondition);
					List<String> dbName = new ArrayList<>();
					if(!CollectionUtils.isEmpty(dbGroupRelPos)){
						for (DbGroupRelPo dbGroupRelPo:dbGroupRelPos
								) {
							DbInfoPo dbInfoPo = dbInfoService.query(dbGroupRelPo.getDbId());
							dbName.add(dbInfoPo.getDbName());
						}
					}
					dbGroupRelDbNameVo.setDbIdList(dbGroupRelPos.stream().map(p->p.getDbId()).collect(Collectors.toList()));
					dbGroupRelDbNameVo.setDbName(dbName);
				}
			}
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}


	@ApiOperation(value = "新增数据库组表",notes = "新增数据库组表",httpMethod = "POST")
	@RequestMapping(value = "/addDbGroup", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DbGroupVo> addDbGroup(@ModelAttribute@Valid  DbGroupCreateForm form) throws BusinessServiceException {
		DbGroupPo po = CopyUtil.transfer(form, DbGroupPo.class);
		Integer groupId =  dbGroupService.queryId();
		po.setId(groupId);
		String userCode = getUserCode();
		po.setCreateUserCode(userCode);
		po.setUpdateUserCode(userCode);
		dbGroupService.insert(po);
		DbGroupVo vo = CopyUtil.transfer(po, DbGroupVo.class);
		List<Integer> dbIds = form.getDbIds();
		if(!CollectionUtils.isEmpty(dbIds)){
			for (Integer dbId:dbIds
					) {
				DbGroupRelPo dbGroupRelPo = new DbGroupRelPo();
				dbGroupRelPo.setDbId(dbId);
				dbGroupRelPo.setGroupId(groupId);
				dbGroupRelService.insert(dbGroupRelPo);
			}
		}
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "新增数据库组表",notes = "新增数据库组表",httpMethod = "POST")
	@RequestMapping(value = "/updateDbGroup", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity updateDbGroup(@ModelAttribute@Valid  DbGroupUpdateForm form) throws BusinessServiceException {
		DbGroupPo po = CopyUtil.transfer(form, DbGroupPo.class);
		String userCode = getUserCode();
		po.setUpdateUserCode(userCode);
		dbGroupService.update(po);
		List<Integer> dbIds = form.getDbIds();
		if(!CollectionUtils.isEmpty(dbIds)){
			DbGroupRelCondition dbGroupRelCondition = new DbGroupRelCondition();
			dbGroupRelCondition.setGroupId(form.getId());
			dbGroupRelService.deleteByGroupId(dbGroupRelCondition);
			for (Integer dbId:dbIds
					) {
				DbGroupRelPo dbGroupRelPo = new DbGroupRelPo();
				dbGroupRelPo.setDbId(dbId);
				dbGroupRelPo.setGroupId(form.getId());
				dbGroupRelService.insert(dbGroupRelPo);
			}
		}
		else {
			DbGroupRelCondition dbGroupRelCondition = new DbGroupRelCondition();
			dbGroupRelCondition.setGroupId(form.getId());
			dbGroupRelService.deleteByGroupId(dbGroupRelCondition);
		}
		return this.getSuccessResult();
	}



	/**
	 * DbGroupQueryForm转换为DbGroupCondition
	 *
	 * @param form
	 * @return
	 */
	private DbGroupCondition getConditionByQueryForm(DbGroupQueryForm form) {
		DbGroupCondition condition = CopyUtil.transfer(form, DbGroupCondition.class);
		return condition;
	}

}